package com.xteam.web.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.bean.BeanHelper;
import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.purview.entities.PurviewOrg;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.service.impl.PurviewOrgFace;
import com.xteam.utils.TreeNodeUtils;
import com.xteam.utils.WebTools;
import com.xteam.web.Error;

public class OrgController extends PurViewController implements IController {

	private PurviewOrgFace	face	= PurviewOrgFace.getInstance();

	public OrgController() {
	}
	
	@Autowired
	private SystemLogService logService;

	/**
	 * 遍历用户所属的组织机构的所有子机构，并返回列表
	 * @author tangkf  ---2012-2-12
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getAllOrgListByUserId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);

		String userId = ReqHelper.getRequestString(request, PurviewUser.PROP_USERID);
		
		//遍历所有
		List<PurviewOrg> orgs = face.getAllOrgListByUserId(userId);
		
		JSONArray jsa = JSONArray.fromObject(orgs);
		PrintWriter out = response.getWriter();
		out.println(jsa.toString());
		return null;
	}

	public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.save(request, response);

		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		String lslMethod = "save";
		
		PurviewOrg fn = new PurviewOrg();
		BeanHelper.requestToBean(fn, request);
		int r = 0;
		
		if (CommonUtils.isEmpty(fn.getOrgId())) {
			r = face.save(fn);
		} else {
			r = face.update(fn);
			lslType = ConstantLocation.LOG_MODIFY;
			lslMethod = "update";
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ORG_MGR, 
				lslMethod, lslType, "PurviewOrg");
		
		Error er = r > 0 ? Error.create(r + "", "保存组织机构出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	/**
	 * 保存用户组织机构关系
	 * @author tangkf ---2012-2-11
	 * @throws Exception
	 */
	public ModelAndView saveUserOrg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		int r = 0;
		String orgId = ReqHelper.getRequestString(request, "orgId");
		String[] userId = ReqHelper.getRequestStrings(request, "userId[]");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		r = face.saveUserOrg(orgId, userId);

		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ORG_MGR, 
				"saveUserOrg", ConstantLocation.LOG_ADDORG_USER, "PurviewUserOrg");
		
		Error er = r > 0 ? Error.create(r + "", "设置用户出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	public ModelAndView delOrgUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		int r = 0;
		String orgId = ReqHelper.getRequestString(request, "orgId");
		String[] userId = ReqHelper.getRequestStrings(request, "userId[]");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		r = face.delOrgUser(orgId, userId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ORG_MGR, 
				"delOrgUser", ConstantLocation.LOG_DELORG_USER, "PurviewUserOrg");

		Error er = r > 0 ? Error.create(r + "", "删除组织机构用户出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.delete(request, response);

		String[] ids = ReqHelper.getRequestStrings(request, PurviewOrg.PROP_ORGID);
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		int r = face.delBatch(PurviewOrg.class, PurviewOrg.PROP_ORGID, ids);

		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ORG_MGR, 
				"delBatch", ConstantLocation.LOG_DELETE_RES, "PurviewOrg");
		
		Error er = r > 0 ? Error.create(r + "", "删除组织机构出错！") : Error.create();
		WebTools.writerJsonToResponse(response, er);
		return null;
	}

	public ModelAndView find(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.find(request, response);
		PrintWriter out = response.getWriter();
		List<PurviewOrg> list = face.queryAll(PurviewOrg.class);
		JSONArray jsa = TreeNodeUtils.getJSONArrayTreeNode(list, "0");
		out.println(jsa.toString());
		return null;
	}

	public ModelAndView getUserListByOrgId(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.setJsonType(request, response);
		String orgId = ReqHelper.getRequestString(request, "orgId");
		PrintWriter out = response.getWriter();
		List<PurviewUser> list = face.getUserListByOrgId(orgId);
		String rinf = "[]";
		if (list != null && list.size() > 0)
			rinf = JSONArray.fromObject(list).toString();
		out.println(rinf);
		return null;
	}
}
